<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <script type="module">
      // Check if the browser has granted us access to 3rd-party storage (such as cookies).
      const hasAccess = await hasStorageAccess();

      if (hasAccess) {
        // If so, attempt to place a cookie to test this assumption.
        attemptWithTestCookie();
      } else {
        // Otherwise, signal that 3rd-party access is not supported.
        signalSupport(false);
      }

      async function hasStorageAccess() {
        if (!("hasStorageAccess" in document)) {
          return true;
        }

        return document.hasStorageAccess();
      }

      function attemptWithTestCookie() {
        // Place a cookie to test whether we can access cookies from 3rd-party storage.
        document.cookie = "KEYCLOAK_3P_COOKIE_SAMESITE=supported; Max-Age=60; SameSite=None; Secure";
        document.cookie = "KEYCLOAK_3P_COOKIE=supported; Max-Age=60";

        // Then redirect to the page where we will read these cookies to confirm this.
        window.location = "step2.html";
      }

      function signalSupport(isSupported) {
        // Signal 3rd-party access support to the page embedding this iframe.
        window.parent.postMessage(isSupported ? "supported" : "unsupported", "*");
      }
    </script>
  </body>
</html>
